import Vue from 'vue'
import Router from 'vue-router'
import Main from '@/view/Main.vue'
// import Home from '@/view/Home'
// import User from '@/view/User'
// import Chat from '@/view/Chat'
// import Login from '@/view/Login'

Vue.use(Router)

// export default new Router({
//   routes: [
//     {
//       path: '/',
//       name: 'Home',
//       component: Home
//     },
//     {
//       path: '/user',
//       name: 'user',
//       component: User
//     },
//     {
//       path: '/chat',
//       name: 'chat',
//       component: Chat
//     }
//   ]
// })

const router = new Router({
  routes:[
    // {
    //   path: '/',
    //   name: 'home',
    //   component:(resolve)=>{
    //     require(['../view/Main'],resolve)
    //   }
    // },
    {
      path: '/',
      name: 'homes',
      component:Main,
      children: [
            {
                path: 'chat',
                name: 'chat',
                component: () => import('@/view/Chat.vue')
            },
            {
              path: 'user',
              name: 'user',
              component:() => import('@/view/User.vue')
            },
            {
              path: 'home',
              name: 'home',
              component:() => import('@/view/Home.vue')
            }
          ]
    },
    {
      path: '/login',
      name: 'login',
      component:(resolve)=>{
        require(['../view/Login'],resolve)
      }
    }
  ]
})

router.beforeEach((to,from,next)=>{
  console.log( localStorage.getItem('auth-token'));
  // console.log(localStorage.getItem('auth-token'));
  if (localStorage.getItem('auth-token') || to.name=='login') {
    next()
  }else{
    next({
      path:'/login'
    })
  }
})

export default router
